package com.jiexinyun.sftp.jsch;

import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

@Slf4j
@Component
public class SftpClient {

    private static String host = "192.168.0.118";

    private static int port = 21;

    private static String username = "ftptest";

    private static String password = "dell@2025";

    /**
     * pathList中只有一个元素，创建单层目录，多个时创建多层级联目录
     * @param pathList
     */
    public static void createPaths(List<String> pathList) {
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;
        try {
            // 创建SSH会话
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            if(session.isConnected()) {
                // 创建SFTP通道
                channelSftp = (ChannelSftp) session.openChannel("sftp");
                channelSftp.connect();
                if(channelSftp.isConnected()) {
                    String remotePath = "/files/工艺图纸/北方汽修/2025071801";
                    Vector<ChannelSftp.LsEntry> files = channelSftp.ls(remotePath);
                    List<String> fileList = new ArrayList<>();
                    for (ChannelSftp.LsEntry entry : files) {
                        if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
                            fileList.add(entry.getFilename());
                        }
                    }
                    log.info("文件列表：{}", fileList);
                }
            }
        } catch (JSchException | SftpException e) {
            log.error("error", e);
        } finally {
            if (channelSftp != null && channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }

    public static void main(String[] args) {
        uploadFile();
    }

    public static void uploadFile() {
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;
        try {
            // 创建SSH会话
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            if(session.isConnected()) {
                // 创建SFTP通道
                channelSftp = (ChannelSftp) session.openChannel("sftp");
                channelSftp.connect();
                if(channelSftp.isConnected()){
                    String remotePath = "/files/工艺图纸/北方汽修/2025071801";
                    Vector<ChannelSftp.LsEntry> files = channelSftp.ls(remotePath);
                    List<String> fileList = new ArrayList<>();
                    for (ChannelSftp.LsEntry entry : files) {
                        if (!entry.getFilename().equals(".") && !entry.getFilename().equals("..")) {
                            fileList.add(entry.getFilename());
                        }
                    }
                    log.info("文件列表：{}", fileList);
                    // 上传文件
//                    String localFilePath = "D:/test.txt";
//                    String remoteFilePath = "/sftp/jiexinyun";
//                    channelSftp.put(localFilePath, remoteFilePath, ChannelSftp.OVERWRITE);
//                    log.info("文件上传成功");
                }
            }
        } catch (JSchException | SftpException e) {
            log.error("error", e);
        } finally {
            if (channelSftp != null && channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }

    public static void downloadFile() {
        JSch jsch = new JSch();
        Session session = null;
        ChannelSftp channelSftp = null;

        try {
            // 创建SSH会话
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            // 创建SFTP通道
            channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // 下载文件
            String remoteFilePath = "";
            String localFilePath = "";
            channelSftp.get(remoteFilePath, localFilePath);

            System.out.println("文件下载成功");
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null && channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
            if (session != null && session.isConnected()) {
                session.disconnect();
            }
        }
    }

}
